<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">+新增角色</el-button>
          <el-table :data="list" style="width: 100%">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  <!-- 序号实化 -->
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="角色名" />
            <el-table-column prop="description" label="描述" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 10, 100, , 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total,sizes,prev, pager, next,jumper"
              :total="page.total"
              @size-change="sizeChange"
              @current-change="currChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <component :is="'Info'" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <DialogItem
      ref="dialogItem"
      :is-show="show"
      :list="list"
      @input="show = $event"
      @currChange="currChange"
      @getData="getData"
    />
  </div>
</template>
<script>
import { sysRole, sysRoleDel } from '@/api/setting'
import DialogItem from './components/dialogItem.vue'
export default {
  components: {
    DialogItem,
    Info: () => import('./components/info.vue')
  },
  data() {
    return {
      page: {
        page: 1,
        pagesize: 10,
        total: 100
      },
      list: [],
      show: false
    }
  },
  created() {
    // 创建完毕后请求
    this.getData()
    // console.log(this.list)
  },
  methods: {
    // 获取页面数据
    async getData() {
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      // console.log(res)
    },
    // 当前页码绑定分页器页码(跳转到指定页面)
    currChange(page) {
      this.page.page = page
      this.getData()
    },
    // 页容量绑定
    sizeChange(size) {
      this.page.pagesize = size
      this.page.page = 1
      // 改变后刷新数据
      this.getData()
    },
    add() {
      this.show = true
    },
    edit(row) {
      this.show = true
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialogItem.mode = 'edit'
    },
    del(row) {
      this.$confirm('确认删除吗?', '提示')
        .then(() => {
          sysRoleDel(row.id)
          this.$message.success('删除成功')
          this.currChange(1)
        })
        .catch(() => {})
      // sysRoleDel(row.id)
    }
  }
}
</script>
<style lang="scss" scoped>
.setting {
  padding: 15px;
}
</style>
